{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Days Between Two Dates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: daysBetweenDates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #日期之间隔几天"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>请你编写一个程序来计算两个日期之间隔了多少天。</p>\n",
    "\n",
    "<p>日期以字符串形式给出，格式为&nbsp;<code>YYYY-MM-DD</code>，如示例所示。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>date1 = &quot;2019-06-29&quot;, date2 = &quot;2019-06-30&quot;\n",
    "<strong>输出：</strong>1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>date1 = &quot;2020-01-15&quot;, date2 = &quot;2019-12-31&quot;\n",
    "<strong>输出：</strong>15\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>给定的日期是&nbsp;<code>1971</code>&nbsp;年到 <code>2100</code>&nbsp;年之间的有效日期。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-days-between-two-dates](https://leetcode.cn/problems/number-of-days-between-two-dates/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-days-between-two-dates](https://leetcode.cn/problems/number-of-days-between-two-dates/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"2019-06-29\"\\n\"2019-06-30\"', '\"2020-01-15\"\\n\"2019-12-31\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        delta = date.fromisoformat(date1) - date.fromisoformat(date2)\n",
    "        return abs(delta.days)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        dt1 = datetime.fromisoformat(date1)\n",
    "        dt2 = datetime.fromisoformat(date2)\n",
    "        return int(abs((dt1 - dt2).total_seconds())) // (60 * 60 * 24)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import date\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date1[:4]),int(date1[5:7]),int(date1[8:]))-date(int(date2[:4]),int(date2[5:7]),int(date2[8:]))).days)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "\n",
    "        month_days=[31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "\n",
    "        def str2int(date):\n",
    "            date_list=date.split('-')\n",
    "            year=int(date_list[0])\n",
    "            # if date_list[1][0]=='0':\n",
    "            #     month=int(date_list[1][1])\n",
    "            # else:\n",
    "            month=int(date_list[1])\n",
    "            day=int(date_list[2])\n",
    "            return year,month,day\n",
    "        \n",
    "        def is_spec(year):\n",
    "            return year%4==0 and (year%400==0 or year%100!=0)\n",
    "\n",
    "        def sum_year(year):\n",
    "            sum_y=0\n",
    "            for i in range(1,year):\n",
    "                if is_spec(i):\n",
    "                    sum_y+=366\n",
    "                else:\n",
    "                    sum_y+=365\n",
    "            return sum_y\n",
    "\n",
    "        def sum_month(year,month):\n",
    "            sum_m=0\n",
    "            for i in range(1,month):\n",
    "                if i==2 and is_spec(year):\n",
    "                    sum_m+=29\n",
    "                else:\n",
    "                    sum_m+=month_days[i-1]\n",
    "            return sum_m\n",
    "        \n",
    "        year1,month1,day1=str2int(date1)\n",
    "        year2,month2,day2=str2int(date2)\n",
    "\n",
    "        sum1_year=sum_year(year1)\n",
    "        sum2_year=sum_year(year2)\n",
    "\n",
    "        sum1_month=sum_month(year1,month1)\n",
    "        sum2_month=sum_month(year2,month2)\n",
    "\n",
    "        res=abs(sum1_year+sum1_month+day1-(sum2_year+sum2_month+day2))\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def days_cou(date):\n",
    "            days=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "            year=int(date[:4])\n",
    "            mon=int(date[5:7])\n",
    "            day=int(date[8:10])\n",
    "            leap_day = 0\n",
    "            leap_day=(year-1972)//4\n",
    "            if (year-1968)%4==0 :\n",
    "                days[2]=29\n",
    "                leap_day -=1\n",
    "                if year%100==0 and year%400 !=0:\n",
    "                    days[2]=28\n",
    "            n_day=(year-1971)*365+leap_day+sum(days[:mon])+day\n",
    "            return(n_day)\n",
    "        one = days_cou(date1)\n",
    "        two = days_cou(date2)\n",
    "        print(one,two)\n",
    "        return abs(one-two)\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date.fromisoformat(date1) - date.fromisoformat(date2)).days)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date2[:4]), int(date2[5:7]), int(date2[8:])) - date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))).days)\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysSameYear(self, year, m_start, d_start, m_end, d_end):\n",
    "        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "            days_per = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        else:\n",
    "            days_per = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if m_start == m_end:\n",
    "            return d_end - d_start\n",
    "        res = days_per[m_start] - d_start + d_end\n",
    "        for i in range(m_start+1, m_end):\n",
    "            res += days_per[i]\n",
    "        return res\n",
    "\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1, date2 = min(date1, date2), max(date1, date2)\n",
    "        y_start, m_start, d_start = map(int, date1.split('-'))\n",
    "        y_end, m_end, d_end = map(int, date2.split('-'))\n",
    "        if y_start == y_end:\n",
    "            return self.daysSameYear(y_start, m_start, d_start, m_end, d_end)\n",
    "        res = self.daysSameYear(y_start, m_start, d_start, 12, 31) + self.daysSameYear(y_end, 1, 1, m_end, d_end) + 1\n",
    "        for year in range(y_start+1, y_end):\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                res += 366\n",
    "            else:\n",
    "                res += 365\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def is_leap_year(year):\n",
    "            # 判断是否为闰年的函数，与之前的示例相同\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        def get_days(date):\n",
    "            year, month, day = int(date[0:4]), int(date[5:7]), int(date[8:10])\n",
    "            days = 0\n",
    "            month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "            if is_leap_year(year):\n",
    "                month_days[1] = 29\n",
    "            for i in range(1970, year):\n",
    "                year_day = 365\n",
    "                if is_leap_year(i):\n",
    "                    year_day = 366\n",
    "                days += year_day\n",
    "            for m in range(month - 1):\n",
    "                days += month_days[m]\n",
    "            days += day\n",
    "            return days\n",
    "        return abs(get_days(date2) - get_days(date1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date2[:4]),int(date2[5:7]),int(date2[8:]))-\n",
    "                    date(int(date1[:4]),int(date1[5:7]),int(date1[8:]))).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import date\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        mydate1 = date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))\n",
    "        mydate2 = date(int(date2[:4]), int(date2[5:7]), int(date2[8:]))\n",
    "        return abs((mydate1-mydate2).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        count1, count2 = 0, 0\n",
    "        year1, year2 = int(date1[:4]), int(date2[:4])\n",
    "        month1, month2 = int(date1[5:7]), int(date2[5:7])\n",
    "        day1, day2 = int(date1[8:]), int(date2[8:])\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31]\n",
    "        count1 = (year1 - 1971) * 365\n",
    "        for i in range (1971, year1):\n",
    "            if i % 4 == 0:\n",
    "                count1 += 1\n",
    "        if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:\n",
    "            if month1 > 2:\n",
    "                count1 += 1\n",
    "        count1 += sum(days[:month1 - 1]) + day1\n",
    "        count2 = (year2 - 1971) * 365\n",
    "        for i in range (1971, year2):\n",
    "            if i % 4 == 0:\n",
    "                count2 += 1\n",
    "        if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0:\n",
    "            if month2 > 2:\n",
    "                count2 += 1\n",
    "        count2 += sum(days[:month2 - 1]) + day2\n",
    "\n",
    "        return abs(count1 - count2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        s1 = date1.split(\"-\")\n",
    "        s2 = date2.split(\"-\")\n",
    "        d1 = datetime.date(int(s1[0]), int(s1[1]), int(s1[2]))\n",
    "        d2 = datetime.date(int(s2[0]), int(s2[1]), int(s2[2]))\n",
    "        return abs(int((d2 - d1).days))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        '''\n",
    "        date2 = date(int(date2[:4]), int(date2[5:7]), int(date2[8:]))\n",
    "        date1 = date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))\n",
    "        print(date1,date2)\n",
    "        print((date1-date2).days)\n",
    "        return abs((date1-date2).days)\n",
    "        '''\n",
    "        def daysFrom1971(Date) -> int:\n",
    "            Date = Date.split('-')\n",
    "            Date = [int(x) for x in Date]\n",
    "            #print(Date)\n",
    "            days = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "            day = (Date[0]-1971)*365 + sum(days[:Date[1]-1]) + Date[2] - 1\n",
    "            if Date[1] > 2:\n",
    "                if Date[0] % 400 == 0:\n",
    "                    day += 1\n",
    "                elif Date[0] % 100 != 0 and Date[0] % 4 == 0:\n",
    "                    day += 1\n",
    "            day += (Date[0]+2-1971)//4\n",
    "            #print(day)\n",
    "            #print((date(Date[0],Date[1],Date[2])-date(1971,1,1)).days)\n",
    "            return day\n",
    "        return abs(daysFrom1971(date1) - daysFrom1971(date2))\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        if date1 > date2:\n",
    "            tmp = date1\n",
    "            date1 = date2\n",
    "            date2 = tmp \n",
    "        \n",
    "        y1, m1, d1 = [int(s) for s in date1.split('-')]\n",
    "        y2, m2, d2 = [int(s) for s in date2.split('-')]\n",
    "        if y1 < y2: \n",
    "            ds2 = d2 + sum([ self.getMonthDays(y2, m) for m in range(1, m2)])\n",
    "            ds1 = self.getYearDays(y1) - (d1 + sum([ self.getMonthDays(y1, m) for m in range(1, m1)]))\n",
    "            return ds2 + ds1 + sum([self.getYearDays(y) for y in range(y1 +1, y2)])\n",
    "        else:\n",
    "            ds2 = d2 + sum([ self.getMonthDays(y2, m) for m in range(1, m2)])\n",
    "            ds1 = (d1 + sum([ self.getMonthDays(y1, m) for m in range(1, m1)]))\n",
    "            return ds2 - ds1 \n",
    "    \n",
    "    def isLeapYear(self, year):\n",
    "        if year % 4 != 0:\n",
    "            return False\n",
    "        else:\n",
    "            if year % 100 == 0:\n",
    "                return year % 400 == 0 \n",
    "            else:\n",
    "                return True\n",
    "    \n",
    "    def getYearDays(self, year):\n",
    "        return 366 if self.isLeapYear(year) else 365\n",
    "    \n",
    "\n",
    "    def getMonthDays(self, year, month):\n",
    "        if month in (1, 3, 5, 7, 8, 10, 12):\n",
    "            return 31\n",
    "        elif month == 2:\n",
    "            return 29 if self.isLeapYear(year) else 28\n",
    "        else:\n",
    "            return 30      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def days_cou(date):\n",
    "            days=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "            year=int(date[:4])\n",
    "            mon=int(date[5:7])\n",
    "            day=int(date[8:10])\n",
    "            leap_day = 0\n",
    "            leap_day=(year-1972)//4\n",
    "            if (year-1968)%4==0 :\n",
    "                days[2]=29\n",
    "                leap_day -=1\n",
    "                if year%100==0 and year%400 !=0:\n",
    "                    days[2]=28\n",
    "            n_day=(year-1971)*365+leap_day+sum(days[:mon])+day\n",
    "            return(n_day)\n",
    "        one = days_cou(date1)\n",
    "        two = days_cou(date2)\n",
    "        #print(one,two)\n",
    "        return abs(one-two)\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import accumulate\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "      leap = list(accumulate([0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]))\n",
    "      nonleap = list(accumulate([0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]))\n",
    "\n",
    "      def absday(date):\n",
    "        y, m, d = map(int, date.split(\"-\"))\n",
    "\n",
    "        days = 0\n",
    "        days += (y - 1971) * 365\n",
    "        days += (y - 1969) // 4\n",
    "\n",
    "        if y % 4 == 0 and y != 2100:\n",
    "          days += leap[m - 1]\n",
    "        else:\n",
    "          days += nonleap[m - 1]\n",
    "\n",
    "        days += d\n",
    "        return days\n",
    "\n",
    "      return abs(absday(date1) - absday(date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs(date(int(date1[:4]), int(date1[5:7]), int(date1[8:10]))-date(int(date2[:4]), int(date2[5:7]), int(date2[8:10]))).days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)\n",
    "\n",
    "    def date_to_int(self, year, month, day):\n",
    "        ans = 0\n",
    "        month_length = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]\n",
    "        while year != 1971 or month != 1 or day != 1:\n",
    "            ans += 1\n",
    "            day -= 1\n",
    "            if day == 0:\n",
    "                month -= 1\n",
    "                day = month_length[month]\n",
    "                if month == 2 and self.leap_year(year):\n",
    "                    day += 1\n",
    "            if month == 0:\n",
    "                year -= 1\n",
    "                month = 12\n",
    "        return ans\n",
    "\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        start_datetime = datetime.strptime(date1, \"%Y-%m-%d\")\n",
    "        end_datetime = datetime.strptime(date2, \"%Y-%m-%d\")\n",
    "        delta = end_datetime - start_datetime\n",
    "        days = abs(delta.days)\n",
    "        return days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def is_leap_year(year):\n",
    "            # 判断是否为闰年的函数，与之前的示例相同\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        def get_days(date):\n",
    "            year, month, day = int(date[0:4]), int(date[5:7]), int(date[8:10])\n",
    "            days = 0\n",
    "            month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "            if is_leap_year(year):\n",
    "                month_days[1] = 29\n",
    "            for i in range(1, year):\n",
    "                year_day = 365\n",
    "                if is_leap_year(i):\n",
    "                    year_day = 366\n",
    "                days += year_day\n",
    "            for m in range(month - 1):\n",
    "                days += month_days[m]\n",
    "            days += day\n",
    "            return days\n",
    "        return abs(get_days(date2) - get_days(date1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for  i in date2.split('-')]\n",
    "        return abs(self.dates(*date1)-self.dates(*date2))\n",
    "\n",
    "    def leap(self,year):\n",
    "        return year %400 ==0 or (year %100 !=0 and year %4 ==0)\n",
    "        \n",
    "    def dates(self,year,month,day):\n",
    "        ans =0\n",
    "        monthday = [31,31,28,31,30,31,30,31,31,30,31,30]\n",
    "        while year != 1971 or month != 1 or day !=1:\n",
    "            ans += 1\n",
    "            day -=1\n",
    "            if day ==0:\n",
    "                month -= 1\n",
    "                day = monthday[month]\n",
    "                if month ==2 and self.leap(year):\n",
    "                    day += 1\n",
    "            if month ==0:\n",
    "                year -=1\n",
    "                month =12\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        d1 = datetime.datetime.strptime(date1 , '%Y-%m-%d')\n",
    "        d2 = datetime.datetime.strptime(date2 , '%Y-%m-%d')\n",
    "        return abs((d2-d1).days)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        delta = date.fromisoformat(date1) - date.fromisoformat(date2)\n",
    "        return abs(delta.days)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return ((year % 400 == 0) or (year % 100 != 0 and year % 4 == 0))\n",
    "    def date_to_int(self, year, month, day):\n",
    "        month_length = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        ans = day - 1\n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_length[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "        ans += 365 * (year - 1971)\n",
    "        ans += (year - 1) // 4 - 1971 // 4\n",
    "        ans -= (year - 1) // 100 - 1971 // 100\n",
    "        ans += (year - 1) // 400 - 1971 // 400\n",
    "        return ans\n",
    "            \n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        day = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        days = 0\n",
    "        if date1 > date2:\n",
    "            date = date1\n",
    "            date1 = date2\n",
    "            date2 = date\n",
    "\n",
    "        days = days + self.dayscount(date2)-self.dayscount(date1)\n",
    "        if date1[:4] != date2[:4]:\n",
    "            for i in range(int(date1[:4]),int(date2[:4])):\n",
    "                days += 365\n",
    "                if self.run1(i):\n",
    "                    days += 1\n",
    "        return days\n",
    "        # if date1[:4] == date2[:4]:\n",
    "        #     if date2[5:7] == date2[5:7]:\n",
    "        #         return int(date2[8:]-date1[8:])\n",
    "        #     else:\n",
    "        #         for i in range(date1[5:7],date2[5:7]):\n",
    "        #             days += day[i-1]\n",
    "        #         days += int(date2[8:])\n",
    "        #         days -= int(date1[8:])\n",
    "        #         return days\n",
    "        # elif date2[:4] - date1[:4] == 1:\n",
    "        #     for i in range(int(date1[5:7]),13):\n",
    "        #         days += day[i]\n",
    "        #     days -= int(date1[8:])\n",
    "        #     for i in range(int(date3[5:7])-1):\n",
    "        #         days += day[i]\n",
    "        #     days += int(date1[8:])\n",
    "\n",
    "        # else: \n",
    "        #     days += (int(date2[:4])-int(date1[:4])-1)*365\n",
    "        #     for i in range(int(date1[:4])+1,int(date2[:4])):\n",
    "        #         if self.run1(i):\n",
    "        #             days += 1\n",
    "    def dayscount(self,date):\n",
    "        day = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        days = 0\n",
    "        if int(date[5:7]) == 1:\n",
    "            return int(date[8:])\n",
    "        else:\n",
    "            for i in range(int(date[5:7])-1):\n",
    "                days += day[i] \n",
    "            days += int(date[8:])\n",
    "        if int(date[5:7]) > 2 and self.run1(int(date[:4])):\n",
    "            days += 1\n",
    "        return days\n",
    "\n",
    "\n",
    "\n",
    "    def run1(self,year):\n",
    "        if year % 100 == 0:\n",
    "            if year % 400 == 0:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        elif year % 4 == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toDay(self, dateStr):\n",
    "        year = int(dateStr[:4])\n",
    "        month = int(dateStr[5:7])\n",
    "        day = int(dateStr[-2:])\n",
    "\n",
    "        if month <= 2:\n",
    "            year -= 1\n",
    "            month += 10\n",
    "        else:\n",
    "            month -= 2\n",
    "\n",
    "        return 365 * year + year // 4 - year // 100 + year // 400 + 30 * month + (3 * month - 1) // 5 + day \n",
    "    def daysBetweenDates(self, date1, date2):\n",
    "        return abs(self.toDay(date1) - self.toDay(date2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = datetime.strptime(date1, '%Y-%m-%d')\n",
    "        date2 = datetime.strptime(date2, '%Y-%m-%d')\n",
    "        res = abs((date1 - date2).days)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import datetime\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        #print(help(datetime))\n",
    "        s1,s2=date1.split('-'),date2.split('-')\n",
    "        d1=datetime.date(int(s1[0]),int(s1[1]),int(s1[2]))\n",
    "        d2=datetime.date(int(s2[0]),int(s2[1]),int(s2[2]))\n",
    "        df=abs(d2-d1) \n",
    "        if df >datetime.timedelta(0):       \n",
    "            res=str(df).split()\n",
    "            return int(res[0])\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def is_leap_year(year):\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        mon31=[1, 3, 5, 7, 8, 10, 12]\n",
    "        mon30=[4, 6, 9, 11]\n",
    "        year1, mon1, day1 = map(int, date1.split('-'))\n",
    "        year2, mon2, day2 = map(int, date2.split('-'))\n",
    "        days=0\n",
    "        def month(month,year):\n",
    "            if month in mon31:\n",
    "                return 31\n",
    "            if month in mon30:\n",
    "                return 30\n",
    "            if month ==2:\n",
    "                if is_leap_year(year):\n",
    "                    return 29\n",
    "                else:\n",
    "                    return 28\n",
    "        if year1<year2:\n",
    "            if year2-year1>1:\n",
    "                for year in range(year1+1,year2):\n",
    "                    if is_leap_year(year):\n",
    "                        days+=366\n",
    "                    else:\n",
    "                        days+=365\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "            else:\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "\n",
    "        else:\n",
    "            if year1>year2:\n",
    "                if year1-year2>1:\n",
    "                    for year in range(year2+1,year1):\n",
    "                        if is_leap_year(year):\n",
    "                            days+=366\n",
    "                        else:\n",
    "                            days+=365\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "                else:\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "            else:\n",
    "                if mon1>mon2:\n",
    "                    for mon in range(mon2+1,mon1):\n",
    "                        days+=month(mon)\n",
    "                    days+=day1+day2\n",
    "                else:\n",
    "                    if mon1<mon2:\n",
    "                        for mon in range(mon2+1,mon1):\n",
    "                            days+=month(mon)\n",
    "                        days+=day1+day2\n",
    "                    else:\n",
    "                        if day1>day2:\n",
    "                            days=day1-day2\n",
    "                        else:\n",
    "                            days=day2-day1\n",
    "        return days\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def is_leap_year(year):\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        mon31=[1, 3, 5, 7, 8, 10, 12]\n",
    "        mon30=[4, 6, 9, 11]\n",
    "        year1, mon1, day1 = map(int, date1.split('-'))\n",
    "        year2, mon2, day2 = map(int, date2.split('-'))\n",
    "        days=0\n",
    "        def month(month,year):\n",
    "            if month in mon31:\n",
    "                return 31\n",
    "            if month in mon30:\n",
    "                return 30\n",
    "            if month ==2:\n",
    "                if is_leap_year(year):\n",
    "                    return 29\n",
    "                else:\n",
    "                    return 28\n",
    "        if year1<year2:\n",
    "            if year2-year1>1:\n",
    "                for year in range(year1+1,year2):\n",
    "                    if is_leap_year(year):\n",
    "                        days+=366\n",
    "                    else:\n",
    "                        days+=365\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "            else:\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "\n",
    "        else:\n",
    "            if year1>year2:\n",
    "                if year1-year2>1:\n",
    "                    for year in range(year2+1,year1):\n",
    "                        if is_leap_year(year):\n",
    "                            days+=366\n",
    "                        else:\n",
    "                            days+=365\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "                else:\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "            else:\n",
    "                if mon1>mon2:\n",
    "                    for mon in range(mon2+1,mon1):\n",
    "                        days+=month(mon)\n",
    "                    days+=day1+day2\n",
    "                else:\n",
    "                    if mon1<mon2:\n",
    "                        for mon in range(mon2+1,mon1):\n",
    "                            days+=month(mon)\n",
    "                        days+=day1+day2\n",
    "                    else:\n",
    "                        if day1>day2:\n",
    "                            days=day1-day2\n",
    "                        else:\n",
    "                            days=day2-day1\n",
    "        return days\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        d1,d2 = date1.split('-'), date2.split('-')\n",
    "        month = [1,3,5,7,8,10,12]\n",
    "\n",
    "        def c_years(yea):\n",
    "            return True if int(yea)%400==0 or (int(yea)%4==0 and int(yea)%100!=0) else False\n",
    "\n",
    "        def c_days(year_, month_, day_,month):\n",
    "            month_days = 0\n",
    "            for i in range(1, int(month_)):\n",
    "                if i in month:\n",
    "                    month_days+=31\n",
    "                elif i ==2:\n",
    "                    if c_years(int(year_)):\n",
    "                        month_days+=29\n",
    "                    else:\n",
    "                        month_days+=28\n",
    "                else:\n",
    "                    month_days+=30\n",
    "            cur = int(day_) + month_days \n",
    "            return cur\n",
    "\n",
    "        if d1[0]==d2[0]:\n",
    "            return abs(c_days(d1[0], d1[1], d1[2],month)-c_days(d2[0], d2[1], d2[2],month))\n",
    "        else:\n",
    "            d_max = max(d1,d2)\n",
    "            d_min = min(d1,d2)\n",
    "            a = c_days(d_max[0], d_max[1], d_max[2],month)\n",
    "            c = c_days(d_min[0], d_min[1], d_min[2],month)\n",
    "            if c_years(d_min[0]):\n",
    "                c = 366-c\n",
    "            else:\n",
    "                c = 365-c\n",
    "            b = 0\n",
    "            i=int(d_min[0])\n",
    "            while int(d_max[0])-int(d_min[0])>1 and i!=int(d_max[0])-1:\n",
    "                i+=1\n",
    "                if c_years(i):\n",
    "                    b+=366\n",
    "                else:\n",
    "                    b+=365\n",
    "            return a+b+c\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))\n",
    "        \n",
    "    \n",
    "    def leap_year(self, year):\n",
    "        return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)\n",
    "    \n",
    "    def date_to_int(self, year, month, day):\n",
    "        month_dic = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        ans = day\n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_dic[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "        while year > 1971:\n",
    "            year -= 1\n",
    "            ans += 365\n",
    "            if self.leap_year(year):\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def is_leap(self,year):\n",
    "        if year%4==0 and year%100 or year%400==0:\n",
    "            return 1\n",
    "        return 0\n",
    "\n",
    "    def get_days(self,year,month):\n",
    "        if month!=2: return self.months[month]\n",
    "        return self.is_leap(year)+28\n",
    "\n",
    "    def get(self,date):\n",
    "        year,month,day=map(int,date.split('-'))\n",
    "        res=0\n",
    "        for i in range(1971,year):\n",
    "            res+=365+self.is_leap(i)\n",
    "        for i in range(1,month):\n",
    "            res+=self.get_days(year,i)\n",
    "        return res+day\n",
    "\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        self.months=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        return abs(self.get(date1)-self.get(date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date2[:4]), int(date2[5:7]), int(date2[8:])) - date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))).days)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        d1=datetime.strptime(date1,\"%Y-%m-%d\")\n",
    "        d2=datetime.strptime(date2,\"%Y-%m-%d\")\n",
    "        return abs((d2-d1).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date2[:4]), int(date2[5:7]), int(date2[8:])) - date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((date(int(date2[:4]), int(date2[5:7]), int(date2[8:])) - date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def toDay(dateStr):\n",
    "            year = int(dateStr[:4])\n",
    "            month = int(dateStr[5:7])\n",
    "            day = int(dateStr[-2:])\n",
    "\n",
    "            if month <= 2:\n",
    "                year -= 1\n",
    "                month += 10\n",
    "            else:\n",
    "                month -= 2\n",
    "\n",
    "            return 365 * year + year // 4 - year // 100 + year // 400 + 30 * month + (3 * month - 1) // 5 + day - 584418\n",
    "        return abs(toDay(date1) - toDay(date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def getdays(year, month, day):\n",
    "            year, month, day = int(year), int(month), int(day)\n",
    "            ans = day - 1\n",
    "            dayOfMonth = [0,31,28,31,30,31,30,31,31,30,31,30]\n",
    "            ans += (year - 1971) * 365 + (year - 1 - 1968) // 4\n",
    "            for i in range(month):\n",
    "                ans += dayOfMonth[i]\n",
    "            if year % 4 == 0 and month > 2 and year != 2100:\n",
    "                ans += 1\n",
    "            return ans\n",
    "        year1, month1, day1 = date1.split('-')\n",
    "        year2, month2, day2 = date2.split('-')\n",
    "        return abs(getdays(year1, month1, day1) - getdays(year2, month2, day2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import datetime\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        data1 = datetime.datetime.strptime(date1, \"%Y-%m-%d\")\n",
    "        date2 = datetime.datetime.strptime(date2, \"%Y-%m-%d\")\n",
    "        delta = data1 - date2\n",
    "        return abs(delta.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        if date1 > date2:\n",
    "            tmp = date1\n",
    "            date1 = date2\n",
    "            date2 = tmp \n",
    "        \n",
    "        y1, m1, d1 = [int(s) for s in date1.split('-')]\n",
    "        y2, m2, d2 = [int(s) for s in date2.split('-')]\n",
    "        if y1 < y2: \n",
    "            ds2 = d2 + sum([ self.getMonthDays(y2, m) for m in range(1, m2)])\n",
    "            ds1 = self.getYearDays(y1) - (d1 + sum([ self.getMonthDays(y1, m) for m in range(1, m1)]))\n",
    "            return ds2 + ds1 + sum([self.getYearDays(y) for y in range(y1 +1, y2)])\n",
    "        else:\n",
    "            ds2 = d2 + sum([ self.getMonthDays(y2, m) for m in range(1, m2)])\n",
    "            ds1 = (d1 + sum([ self.getMonthDays(y1, m) for m in range(1, m1)]))\n",
    "            return ds2 - ds1 \n",
    "    \n",
    "    def isLeapYear(self, year):\n",
    "        if year % 4 != 0:\n",
    "            return False\n",
    "        else:\n",
    "            if year % 100 == 0:\n",
    "                return year % 400 == 0 \n",
    "            else:\n",
    "                return True\n",
    "    \n",
    "    def getYearDays(self, year):\n",
    "        return 366 if self.isLeapYear(year) else 365\n",
    "    \n",
    "\n",
    "    def getMonthDays(self, year, month):\n",
    "        if month in (1, 3, 5, 7, 8, 10, 12):\n",
    "            return 31\n",
    "        elif month == 2:\n",
    "            return 29 if self.isLeapYear(year) else 28\n",
    "        else:\n",
    "            return 30      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = datetime.strptime(date1,'%Y-%m-%d')\n",
    "        date2 = datetime.strptime(date2,'%Y-%m-%d')\n",
    "        res = abs((date1 - date2).days)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def daysBetweenDates(self,  date1: str, date2: str) -> int:\n",
    "        month_days = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        def is_leap(year):\n",
    "            return year % 4 == 0 and (year %100!=0 or year % 400==0) \n",
    "\n",
    "        def calculate_two_years(year1, year2):\n",
    "            return sum(366 if is_leap(year) else 365 for year in range(year1, year2))\n",
    "        \n",
    "        def calculate_from_year_start(year, month, day):\n",
    "            return sum(month_days[:(month-1)]) + day + (1 if is_leap(year) and month >2 else 0 )\n",
    "        \n",
    "        d1, d2 = min(date1, date2), max(date1, date2)\n",
    "        year1, month1, day1 = (int(i) for i in d1.split('-'))\n",
    "        year2, month2, day2 = (int(i) for i in d2.split('-'))\n",
    "\n",
    "        return calculate_two_years(year1, year2) + calculate_from_year_start(year2, month2, day2) - calculate_from_year_start(year1, month1, day1)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def get_days(year,month,day):\n",
    "            months=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "            for i in range(1,13):\n",
    "                months[i]+=months[i-1]\n",
    "            result=(year-1-1968)*365+(year-1-1968)//4\n",
    "            if month>2 and year!=2100 and year%4==0:\n",
    "                result+=1\n",
    "            result+=months[month-1]+day\n",
    "\n",
    "            return result\n",
    "        \n",
    "        p=date1.split('-')\n",
    "        year,month,day=int(p[0]),int(p[1]),int(p[2])\n",
    "        days1=get_days(year,month,day)\n",
    "        \n",
    "        p=date2.split('-')\n",
    "        year,month,day=int(p[0]),int(p[1]),int(p[2])\n",
    "        days2=get_days(year,month,day)\n",
    "\n",
    "        return abs(days2-days1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)\n",
    "\n",
    "    def datetodays(self, year, month, day):\n",
    "        ans = 0\n",
    "        month_length = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]\n",
    "        while year != 1971 or month != 1 or day != 1:\n",
    "            ans += 1\n",
    "            day -= 1\n",
    "            if day == 0:\n",
    "                month -= 1\n",
    "                day = month_length[month]\n",
    "                if month == 2 and self.leap_year(year):\n",
    "                    day += 1\n",
    "            if month == 0:\n",
    "                year -= 1\n",
    "                month = 12\n",
    "        return ans\n",
    "\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        year1, mon1, day1 = map(int, date1.split(\"-\"))\n",
    "        year2, mon2, day2 = map(int, date2.split(\"-\"))\n",
    "        return abs(self.datetodays(year1,mon1,day1) - self.datetodays(year2,mon2,day2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return ((year % 400 == 0) or (year % 100 != 0 and year % 4 == 0))\n",
    "    def date_to_int(self, year, month, day):\n",
    "        month_length = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        ans = day - 1\n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_length[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "        ans += 365 * (year - 1971)\n",
    "        ans += (year - 1) // 4 - 1971 // 4\n",
    "        ans -= (year - 1) // 100 - 1971 // 100\n",
    "        ans += (year - 1) // 400 - 1971 // 400\n",
    "        return ans\n",
    "            \n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        # 非整百年：能被4整除而不能被100整除的为闰年\n",
    "        # 整百年：能被400整除而不能被3200整除的是闰年\n",
    "        return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)\n",
    "\n",
    "    def date_to_int(self, year, month, day):\n",
    "        ans = day - 1\n",
    "        month_length = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30] # 正常来说每个月有几天\n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_length[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "    \n",
    "        ans += 365 * (year - 1971)\n",
    "        ans += (year - 1) // 4 - 1971 // 4\n",
    "        ans -= (year - 1) // 100 - 1971 // 100\n",
    "        ans += (year - 1) // 400 - 1971 // 400\n",
    "        return ans\n",
    "            \n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        start = datetime.strptime(date1, \"%Y-%m-%d\")\n",
    "        end = datetime.strptime(date2, \"%Y-%m-%d\")\n",
    "        return abs((end-start).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return ((year % 400 == 0) or ((year % 100) != 0 and (year % 4) == 0))\n",
    "\n",
    "    def date_to_int(self, year, month, day):\n",
    "        month_len = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        ans = day \n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_len[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "        ans += 365 * (year - 1971)\n",
    "        ans += (year-1)//4 - 1971//4\n",
    "        ans -= (year-1)//100 - 1971//100\n",
    "        ans += (year-1)//400 - 1971//400\n",
    "        return ans\n",
    "\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1)-self.date_to_int(*date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        dateFormat = '%Y-%m-%d'\n",
    "        dateObj1 = datetime.strptime(date1, dateFormat)\n",
    "        dateObj2 = datetime.strptime(date2, dateFormat)\n",
    "        return abs((dateObj1 - dateObj2).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        \n",
    "#         month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]  # 平年的 12 个月的天数\n",
    "        \n",
    "#         d1, d2 = min(date1, date2), max(date1, date2)  # d1 为较小的日期，d2 为较大的日期\n",
    "#         year1, month1, day1 = (int(i) for i in d1.split('-'))\n",
    "#         year2, month2, day2 = (int(i) for i in d2.split('-'))\n",
    "\n",
    "#         def is_leap(year):\n",
    "#             # 判断是否为闰年\n",
    "#             return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)\n",
    "        \n",
    "#         def days_between_two_years(year1, year2):\n",
    "#             # 两个年份之间的间隔天数\n",
    "#             return sum(366 if is_leap(year) else 365 for year in range(year1, year2))\n",
    "        \n",
    "#         def days_from_year_start(year, month, day):\n",
    "#             # 从当年的1月1号到当天的天数，对于闰年且过了2月的情况，要额外补偿1天\n",
    "#             return sum(month_days[:(month-1)]) + day + (1 if is_leap(year) and month > 2 else 0)\n",
    "#         print(days_from_year_start(year1, month1, day1), days_from_year_start(year2, month2, day2))\n",
    "#         return  days_between_two_years(year1, year2) + days_from_year_start(year2, month2, day2) - days_from_year_start(year1, month1, day1)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = date1.split(\"-\")\n",
    "        date2 = date2.split(\"-\")\n",
    "        date1 = [int(date1[0]), int(date1[1]), int(date1[2])]\n",
    "        date2 = [int(date2[0]), int(date2[1]), int(date2[2])]\n",
    "        if date1[0] > date2[0] or (date1[0] == date2[0] and date1[1] > date2[1]) or (date1[0] == date2[0] and date1[1] == date2[1] and date1[2] > date2[2]):\n",
    "            date1, date2 = date2, date1\n",
    "        print(date1, date2)\n",
    "        def isrun(year):\n",
    "            if year % 100 == 0:\n",
    "                if year % 400 == 0:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            if year % 4 == 0:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "            \n",
    "\n",
    "        month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        day1 = date1[2]\n",
    "        for m in range(date1[1]-1):\n",
    "            day1 += month_day[m]\n",
    "        if date1[1] > 2 and isrun(date1[0]):\n",
    "            day1 += 1\n",
    "        day2 = date2[2]\n",
    "        for m in range(date2[1]-1):\n",
    "            day2 += month_day[m]\n",
    "        if date2[1] > 2 and isrun(date2[0]):\n",
    "            day2 += 1\n",
    "        print(day1, day2)\n",
    "        for y in range(date1[0], date2[0]):\n",
    "            if isrun(y):\n",
    "                # print(y)\n",
    "                day2 += 366\n",
    "            else:\n",
    "                day2 += 365\n",
    "        print(day2)\n",
    "        return day2 - day1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "\n",
    "\n",
    "        y1,m1,d1=map(int,date1.split(\"-\")) \n",
    "        y2,m2,d2=map(int,date2.split(\"-\")) \n",
    "\n",
    "        return abs((date(y1,m1,d1)-date(y2,m2,d2)).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def process(s):\n",
    "    arr = s.split('-')\n",
    "    if len(arr) != 3:\n",
    "        return 0, 0, 0\n",
    "    return int(arr[0]), int(arr[1]), int(arr[2])\n",
    "\n",
    "\n",
    "def isRunYear(year):\n",
    "    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "\n",
    "\n",
    "monthday = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        if date1 < date2:\n",
    "            date1, date2 = date2, date1\n",
    "        y1, m1, d1 = process(date1)\n",
    "        y2, m2, d2 = process(date2)\n",
    "\n",
    "        days = 0\n",
    "        for y in range(y2, y1):\n",
    "            if isRunYear(y):\n",
    "                days += 366\n",
    "            else:\n",
    "                days += 365\n",
    "\n",
    "        if days > 0 and isRunYear(y2) and m2 > 2:\n",
    "            days -= 1\n",
    "        if isRunYear(y1) and m1 > 2:\n",
    "            days += 1\n",
    "\n",
    "        isNegtive = False\n",
    "        if m1 < m2:\n",
    "            isNegtive = True\n",
    "            m1, m2 = m2, m1\n",
    "        tmp = 0\n",
    "        for m in range(m2, m1):\n",
    "            tmp += monthday[m - 1]\n",
    "\n",
    "        if not isNegtive:\n",
    "            days += tmp\n",
    "        else:\n",
    "            days -= tmp\n",
    "\n",
    "        days += d1 - d2\n",
    "\n",
    "        return days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        import datetime\n",
    "        d1 = [int(i) for i in date1.split(\"-\")]\n",
    "        d2 = [int(i) for i in date2.split(\"-\")]\n",
    "        d1 = datetime.datetime(d1[0],d1[1],d1[2])   # 第一个日期\n",
    "        d2 = datetime.datetime(d2[0],d2[1],d2[2])   # 第二个日期\n",
    "        interval = d2 - d1                   # 两日期差距\n",
    "        return abs(interval.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        import datetime\n",
    "\n",
    "        d1 = datetime.datetime.strptime(date1, \"%Y-%m-%d\")\n",
    "        d2 = datetime.datetime.strptime(date2, \"%Y-%m-%d\")\n",
    "\n",
    "        day = d2 - d1\n",
    "        return abs(day.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from dataclasses import dataclass\n",
    "\n",
    "def is_leap(year: int) -> bool:\n",
    "    if year % 100 == 0:\n",
    "        return year % 400 == 0\n",
    "    return year % 4 == 0\n",
    "\n",
    "daysOfYear = [366 if is_leap(year) else 365 for year in range(1971, 2100)]\n",
    "daysOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "daysOfMonth_Leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "@dataclass\n",
    "class Date:\n",
    "    year: int\n",
    "    month: int\n",
    "    day: int\n",
    "\n",
    "def strptime(s):\n",
    "    year, month, day = map(int, s.split(\"-\"))\n",
    "    return Date(year, month, day)\n",
    "\n",
    "def daysFromInit(date):\n",
    "    init_year = 1971\n",
    "    init_date = Date(init_year, 1, 1)\n",
    "    days_by_years = sum(daysOfYear[(init_date.year-init_year):(date.year-init_year)])\n",
    "    days = daysOfMonth_Leap if is_leap(date.year) else daysOfMonth\n",
    "    days_of_current_date = sum(days[:(date.month-1)]) + date.day\n",
    "    return days_by_years + days_of_current_date\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "      date1 = strptime(date1)\n",
    "      date2 = strptime(date2)\n",
    "      return abs(daysFromInit(date1) - daysFromInit(date2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        if date1 < date2:\n",
    "            date1, date2 = date2, date1\n",
    "\n",
    "        return (datetime.strptime(date1, \"%Y-%m-%d\") - datetime.strptime(date2, \"%Y-%m-%d\")).days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        '''\n",
    "        date2 = date(int(date2[:4]), int(date2[5:7]), int(date2[8:]))\n",
    "        date1 = date(int(date1[:4]), int(date1[5:7]), int(date1[8:]))\n",
    "        print(date1,date2)\n",
    "        print((date1-date2).days)\n",
    "        return abs((date1-date2).days)\n",
    "        '''\n",
    "        def daysFrom1971(Date) -> int:\n",
    "            Date = Date.split('-')\n",
    "            Date = [int(x) for x in Date]\n",
    "            print(Date)\n",
    "            days = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "            day = (Date[0]-1971)*365 + sum(days[:Date[1]-1]) + Date[2] - 1\n",
    "            if Date[1] > 2:\n",
    "                if Date[0] % 400 == 0:\n",
    "                    day += 1\n",
    "                elif Date[0] % 100 != 0 and Date[0] % 4 == 0:\n",
    "                    day += 1\n",
    "            day += (Date[0]+2-1971)//4\n",
    "            print(day)\n",
    "            print((date(Date[0],Date[1],Date[2])-date(1971,1,1)).days)\n",
    "            return day\n",
    "        return abs(daysFrom1971(date1) - daysFrom1971(date2))\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def is_leap_year(year):\n",
    "            if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        mon31=[1, 3, 5, 7, 8, 10, 12]\n",
    "        mon30=[4, 6, 9, 11]\n",
    "        year1, mon1, day1 = map(int, date1.split('-'))\n",
    "        year2, mon2, day2 = map(int, date2.split('-'))\n",
    "        days=0\n",
    "        def month(month,year):\n",
    "            if month in mon31:\n",
    "                return 31\n",
    "            if month in mon30:\n",
    "                return 30\n",
    "            if month ==2:\n",
    "                if is_leap_year(year):\n",
    "                    return 29\n",
    "                else:\n",
    "                    return 28\n",
    "        if year1<year2:\n",
    "            if year2-year1>1:\n",
    "                for year in range(year1+1,year2):\n",
    "                    if is_leap_year(year):\n",
    "                        days+=366\n",
    "                    else:\n",
    "                        days+=365\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "            else:\n",
    "                for mon in range(mon1,13):\n",
    "                    days+=month(mon,year1)-day1\n",
    "                    day1=0\n",
    "                for mon in range(1,mon2):\n",
    "                    days+=month(mon,year2)\n",
    "                days+=day2\n",
    "\n",
    "        else:\n",
    "            if year1>year2:\n",
    "                if year1-year2>1:\n",
    "                    for year in range(year2+1,year1):\n",
    "                        if is_leap_year(year):\n",
    "                            days+=366\n",
    "                        else:\n",
    "                            days+=365\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "                else:\n",
    "                    for mon in range(mon2,13):\n",
    "                        days+=month(mon,year2)-day2\n",
    "                        day2=0\n",
    "                    for mon in range(1,mon1):\n",
    "                        days+=month(mon,year1)\n",
    "                    days+=day1\n",
    "            else:\n",
    "                if mon1>mon2:\n",
    "                    for mon in range(mon2+1,mon1):\n",
    "                        days+=month(mon)\n",
    "                    days+=day1+day2\n",
    "                else:\n",
    "                    if mon1<mon2:\n",
    "                        for mon in range(mon2+1,mon1):\n",
    "                            days+=month(mon)\n",
    "                        days+=day1+day2\n",
    "                    else:\n",
    "                        if day1>day2:\n",
    "                            days=day1-day2\n",
    "                        else:\n",
    "                            days=day2-day1\n",
    "        return days\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        \n",
    "        month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]  # 平年的 12 个月的天数\n",
    "        \n",
    "        d1, d2 = min(date1, date2), max(date1, date2)  # d1 为较小的日期，d2 为较大的日期\n",
    "        year1, month1, day1 = (int(i) for i in d1.split('-'))\n",
    "        year2, month2, day2 = (int(i) for i in d2.split('-'))\n",
    "\n",
    "        def is_leap(year):\n",
    "            # 判断是否为闰年\n",
    "            return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)\n",
    "        \n",
    "        def days_between_two_years(year1, year2):\n",
    "            # 两个年份之间的间隔天数\n",
    "            return sum(366 if is_leap(year) else 365 for year in range(year1, year2))\n",
    "        \n",
    "        def days_from_year_start(year, month, day):\n",
    "            # 从当年的1月1号到当天的天数，对于闰年且过了2月的情况，要额外补偿1天\n",
    "            return sum(month_days[:(month-1)]) + day + (1 if is_leap(year) and month > 2 else 0)\n",
    "\n",
    "        return  days_between_two_years(year1, year2) + days_from_year_start(year2, month2, day2) - days_from_year_start(year1, month1, day1)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# class Solution:\n",
    "#     def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "#         date1 = date1.split(\"-\")\n",
    "#         date2 = date2.split(\"-\")\n",
    "#         date1 = [int(date1[0]), int(date1[1]), int(date1[2])]\n",
    "#         date2 = [int(date2[0]), int(date2[1]), int(date2[2])]\n",
    "#         if date1[0] > date2[0] or (date1[0] == date2[0] and date1[1] > date2[1]) or (date1[0] == date2[0] and date1[1] == date2[1] and date1[2] > date2[2]):\n",
    "#             date1, date2 = date2, date1\n",
    "#         print(date1, date2)\n",
    "#         def isrun(year):\n",
    "#             if year % 100 == 0:\n",
    "#                 if year % 400 == 0:\n",
    "#                     return True\n",
    "#                 else:\n",
    "#                     return False\n",
    "#             if year % 4 == 0:\n",
    "#                 return True\n",
    "#             else:\n",
    "#                 return False\n",
    "            \n",
    "\n",
    "#         month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "#         day1 = date1[2]\n",
    "#         for m in range(date1[1]):\n",
    "#             day1 += month_day[m]\n",
    "#         if date1[1] > 2 and isrun(date1[0]):\n",
    "#             day1 += 1\n",
    "#         day2 = date2[2]\n",
    "#         for m in range(date2[1]):\n",
    "#             day2 += month_day[m]\n",
    "#         if date2[1] > 2 and isrun(date2[0]):\n",
    "#             day2 += 1\n",
    "#         print(day1, day2)\n",
    "#         for y in range(date1[0], date2[0]):\n",
    "#             if isrun(y):\n",
    "#                 # print(y)\n",
    "#                 day2 += 366\n",
    "#             else:\n",
    "#                 day2 += 365\n",
    "#         print(day2)\n",
    "#         return day2 - day1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        d1,d2 = date1.split('-'), date2.split('-')\n",
    "        month = [1,3,5,7,8,10,12]\n",
    "\n",
    "        def c_days(year_, month_, day_,month):\n",
    "            month_days = 0\n",
    "            for i in range(1, int(month_)):\n",
    "                if i in month:\n",
    "                    month_days+=31\n",
    "                elif i ==2:\n",
    "                    if int(year_)%400==0 or (int(year_)%4==0 and int(year_)%100!=0):\n",
    "                        month_days+=29\n",
    "                    else:\n",
    "                        month_days+=28\n",
    "                else:\n",
    "                    month_days+=30\n",
    "            cur = int(day_) + month_days \n",
    "            return cur\n",
    "\n",
    "        if d1[0]==d2[0]:\n",
    "            return abs(c_days(d1[0], d1[1], d1[2],month)-c_days(d2[0], d2[1], d2[2],month))\n",
    "        else:\n",
    "            d_max = max(d1,d2)\n",
    "            d_min = min(d1,d2)\n",
    "            a = c_days(d_max[0], d_max[1], d_max[2],month)\n",
    "            c = c_days(d_min[0], d_min[1], d_min[2],month)\n",
    "            if int(d_min[0])%400==0 or int(d_min[0])%4==0 and int(d_min[0])%100!=0:\n",
    "                c = 366-c\n",
    "            else:\n",
    "                c = 365-c\n",
    "            b = 0\n",
    "            i=int(d_min[0])\n",
    "            while int(d_max[0])-int(d_min[0])>1 and i!=int(d_max[0])-1:\n",
    "                i+=1\n",
    "                if i%400==0 or (i%4==0 and i%100!=0):\n",
    "                    b+=366\n",
    "                else:\n",
    "                    b+=365\n",
    "            return a+b+c\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1)-self.date_to_int(*date2))\n",
    "    \n",
    "    def leap_year(self, year):\n",
    "        return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)\n",
    "    \n",
    "    def date_to_int(self, year, month, day):\n",
    "        ans = 0\n",
    "        month_length = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]\n",
    "        while year != 1971 or month != 1 or day != 1:\n",
    "            ans += 1\n",
    "            day -= 1\n",
    "            if day == 0:\n",
    "                month -= 1\n",
    "                day = month_length[month]\n",
    "                if month == 2 and self.leap_year(year):\n",
    "                    day += 1\n",
    "            if month == 0:\n",
    "                year -= 1\n",
    "                month = 12\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((datetime.datetime.strptime(date2, '%Y-%m-%d') - datetime.datetime.strptime(date1, '%Y-%m-%d')).days)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        d1 = datetime.datetime.strptime(date1, '%Y-%m-%d')\n",
    "        d2 = datetime.datetime.strptime(date2, '%Y-%m-%d')\n",
    "        delta = d2 - d1\n",
    "        return abs(delta.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\r\n",
    "        DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\r\n",
    "        date1, date2 = min(date1, date2), max(date1, date2)\r\n",
    "        year1, month1, day1 = map(int, date1.split('-'))\r\n",
    "        year2, month2, day2 = map(int, date2.split('-'))\r\n",
    "        days = 0\r\n",
    "        for y in range(year1 + 1, year2):\r\n",
    "            if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:\r\n",
    "                days += 366\r\n",
    "            else:\r\n",
    "                days += 365\r\n",
    "        if year1 != year2:\r\n",
    "            for m in range(month1, 13):\r\n",
    "                days += DAYS[m - 1]\r\n",
    "                if m == 2:\r\n",
    "                    if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:\r\n",
    "                        days += 1\r\n",
    "            days -= day1\r\n",
    "            for m in range(1, month2):\r\n",
    "                days += DAYS[m - 1]\r\n",
    "                print(DAYS[m - 1])\r\n",
    "                if m == 2:\r\n",
    "                    if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0:\r\n",
    "                        days += 1\r\n",
    "            days += day2\r\n",
    "        else:\r\n",
    "            for m in range(month1, month2):\r\n",
    "                days += DAYS[m - 1]\r\n",
    "                if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:\r\n",
    "                    days += 1\r\n",
    "            days += day2 - day1\r\n",
    "        return days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime \n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        return abs((datetime.strptime(date1,'%Y-%m-%d') - datetime.strptime(date2,'%Y-%m-%d')).days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leap_year(self, year):\n",
    "        return ((year % 400 == 0) or (year % 100 != 0 and year % 4 == 0))\n",
    "    def date_to_int(self, year, month, day):\n",
    "        month_length = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        ans = day - 1\n",
    "        while month != 0:\n",
    "            month -= 1\n",
    "            ans += month_length[month]\n",
    "            if month == 2 and self.leap_year(year):\n",
    "                ans += 1\n",
    "        ans += 365 * (year - 1971)\n",
    "        ans += (year - 1) // 4 - 1971 // 4\n",
    "        ans -= (year - 1) // 100 - 1971 // 100\n",
    "        ans += (year - 1) // 400 - 1971 // 400\n",
    "        return ans\n",
    "            \n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        date1 = [int(i) for i in date1.split('-')]\n",
    "        date2 = [int(i) for i in date2.split('-')]\n",
    "        return abs(self.date_to_int(*date1) - self.date_to_int(*date2))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        def isLeapYear(year: int) -> bool:\n",
    "            return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)\n",
    "\n",
    "        def daysInMonth(year: int, month: int) -> int:\n",
    "            days = [\n",
    "                31,\n",
    "                28 + int(isLeapYear(year)),\n",
    "                31,\n",
    "                30,\n",
    "                31,\n",
    "                30,\n",
    "                31,\n",
    "                31,\n",
    "                30,\n",
    "                31,\n",
    "                30,\n",
    "                31,\n",
    "            ]\n",
    "            return days[month - 1]\n",
    "\n",
    "        def calcDays(date: str) -> int:\n",
    "            year, month, day =  map(int, date.split(\"-\"))\n",
    "            days = 0\n",
    "            for y in range(1971, year):\n",
    "                days += 365 + int(isLeapYear(y))\n",
    "            for m in range(1, month):\n",
    "                days += daysInMonth(year, m)\n",
    "            days += day\n",
    "            return days\n",
    "\n",
    "        return abs(calcDays(date1) - calcDays(date2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def daysBetweenDates(self, date1: str, date2: str) -> int:\n",
    "        if date1 > date2:\n",
    "            tmp = date1\n",
    "            date1 = date2\n",
    "            date2 = tmp \n",
    "        \n",
    "        y1, m1, d1 = [int(s) for s in date1.split('-')]\n",
    "        y2, m2, d2 = [int(s) for s in date2.split('-')]\n",
    "        if y1 == y2 and m1 == m2:\n",
    "            return d2 - d1 \n",
    "            \n",
    "\n",
    "        s = self.getMonthDays(y1, m1) - d1 \n",
    "        m1 += 1\n",
    "        if m1 > 12:\n",
    "            y1 += 1\n",
    "            m1 = 1\n",
    "        while y1 < y2 or m1 < m2: \n",
    "            s += self.getMonthDays(y1, m1)\n",
    "            m1 += 1\n",
    "            if m1 > 12:\n",
    "                y1 += 1\n",
    "                m1 = 1\n",
    "        s += d2\n",
    "        return s \n",
    "        \n",
    "    \n",
    "    def isLeapYear(self, year):\n",
    "        if year % 4 != 0:\n",
    "            return False\n",
    "        else:\n",
    "            if year % 100 == 0:\n",
    "                return year % 400 == 0 \n",
    "            else:\n",
    "                return True\n",
    "    \n",
    "    def getYearDays(self, year):\n",
    "        return 366 if self.isLeapYear(year) else 365\n",
    "    \n",
    "\n",
    "    def getMonthDays(self, year, month):\n",
    "        if month in (1, 3, 5, 7, 8, 10, 12):\n",
    "            return 31\n",
    "        elif month == 2:\n",
    "            return 29 if self.isLeapYear(year) else 28\n",
    "        else:\n",
    "            return 30      \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
